package cn.xiongxf.config;

import cn.xiongxf.domain.Permission;
import cn.xiongxf.mapper.PermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.List;

@Configuration
@EnableWebSecurity  // 开启Security
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private PermissionMapper permissionMapper;

//    @Bean
//    public UserDetailsService userDetailsService() {
//        return new SKCCUserDetailService();  // 用自己写的
//    }

    // Security通过PasswordEncoder对比 用户输入的密码和数据库的是否匹配
    @Bean
    public PasswordEncoder passwordEncoder() {
//        return NoOpPasswordEncoder.getInstance();  // 密码编码器NoOpPasswordEncoder不加密
        return new BCryptPasswordEncoder();
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        // 同样的密码每次编码会生成不同的加密密码
        String encode1 = bCryptPasswordEncoder.encode("123456");
        String encode2 = bCryptPasswordEncoder.encode("123456");
        System.out.println(encode1);
        System.out.println(encode2);
        // 不同的加密密码能匹配一样的密码
        boolean b1 = bCryptPasswordEncoder.matches("123456", "$2a$10$x5VCK4z1CTuZqmvH3gVT9OitEMytNacoerNPu5Ws2teKBVdYopXSy");
        boolean b2 = bCryptPasswordEncoder.matches("123456", "$2a$10$JjVa2RHUyAQeDL/eBtFaxuwiJ.QJLsMKLVwgi1SMYOPypE7ssllUS");
        System.out.println(b1);
        System.out.println(b2);
    }

    // 授权规则配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        // 查询所有权限
//        List<Permission> permissions = permissionMapper.selectList(null);
//        permissions.forEach(permission -> {
//            try {
//                http.authorizeRequests().antMatchers(permission.getResource()).hasAuthority(permission.getSn());
//            } catch (Exception e) {
//                e.printStackTrace();
//                throw new RuntimeException(e);
//            }
//        });

        // 授权配置
        http.authorizeRequests()
                .antMatchers("/login").permitAll()  // 登录路径放行
                .anyRequest().authenticated()                    // 其他路径都要认证之后才能访问
                .and().formLogin()                               // 允许表单登录
                .successForwardUrl("/login/loginSuccess")        // 设置登陆成功
                .and().logout().permitAll()                      // 登出路径放行 /logout
                .and().csrf().disable();                         // 关闭跨域伪造检查
    }

    // 配置认证管理器，授权模式为“poassword”时会用到
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

}